<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="学校名称" prop="schoolName">
        <el-input v-model="queryParams.schoolName" placeholder="请输入学校名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="学校地址" prop="schoolAddress">
        <el-input v-model="queryParams.schoolAddress" placeholder="请输入学校地址" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="电子邮箱" prop="schoolEmil">
        <el-input v-model="queryParams.schoolEmil" placeholder="请输入电子邮箱" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="上级部门" prop="upDepartment">
        <el-cascader v-model="queryParams.upDepartment" clearable :options="deptTree" :props="{label:'deptName',value:'deptId',emitPath:false,checkStrictly:true}"></el-cascader>
      </el-form-item>
      <el-form-item label="所属辖区" prop="jurisdiction">
        <el-input v-model="queryParams.jurisdiction" placeholder="请输入所属辖区" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="联系人" prop="contacts">
        <el-input v-model="queryParams.contacts" placeholder="请输入联系人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="固定电话" prop="fixedTelephone">
        <el-input v-model="queryParams.fixedTelephone" placeholder="请输入固定电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="联系人电话" prop="contactsPhone">
        <el-input v-model="queryParams.contactsPhone" placeholder="请输入联系人电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="邮政编码" prop="postalCode">
        <el-input v-model="queryParams.postalCode" placeholder="请输入邮政编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:school:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['system:school:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['system:school:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['system:school:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="schoolList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学校名称" align="center" prop="schoolName" width="160"/>
      <el-table-column label="学校类型" align="center" prop="schoolType" width="80">
        <template slot-scope="scope">
          {{selectDictLabel(dict.type.school_type,scope.row.schoolType)}}
        </template>
      </el-table-column>
      <el-table-column label="学校地址" align="center" prop="schoolAddress" />
      <el-table-column label="电子邮箱" align="center" prop="schoolEmil" width="140"/>
      <el-table-column label="上级部门" align="center" prop="upDepartment" width="130">
        <template slot-scope="scope">{{selectDictLabel(deptList,scope.row.upDepartment,"deptId","deptName")}} </template>
      </el-table-column>
      <el-table-column label="所属辖区" align="center" prop="jurisdiction" width="120"/>
      <el-table-column label="联系人" align="center" prop="contacts" width="140"/>
      <el-table-column label="固定电话" align="center" prop="fixedTelephone" width="120"/>
      <el-table-column label="联系人电话" align="center" prop="contactsPhone" width="120"/>
      <el-table-column label="邮政编码" align="center" prop="postalCode" width="80"/>
      <el-table-column label="学校网站" align="center" prop="webPage" />
      <!-- <el-table-column label="状态" align="center" prop="status" /> -->

      <el-table-column label="操作" align="center" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:school:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:school:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改学校对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="学校名称" prop="schoolName">
          <el-input v-model="form.schoolName" placeholder="请输入学校名称" />
        </el-form-item>
        <el-form-item label="学校类型" prop="schoolType">
          <el-select v-model="form.schoolType" placeholder="请选择学校类型" style="width:100%">
            <el-option v-for="dict in dict.type.school_type" :key="dict.value" :label="dict.label" :value="Number(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学校地址" prop="schoolAddress">
          <el-input v-model="form.schoolAddress" placeholder="请输入学校地址" />
        </el-form-item>
        <el-form-item label="学校简介" prop="schoolProfile">
          <el-input v-model="form.schoolProfile" type="textarea" placeholder="请输入学校简介" autosize :autosize="{ minRows: 2, maxRows: 10}" />
        </el-form-item>
        <el-form-item label="电子邮箱" prop="schoolEmil">
          <el-input v-model="form.schoolEmil" placeholder="请输入电子邮箱" />
        </el-form-item>
        <el-form-item label="上级部门" prop="upDepartment">
          <el-cascader v-model="form.upDepartment" style="width:100%" :options="deptTree" :props="{label:'deptName',value:'deptId',emitPath:false,checkStrictly:true}"></el-cascader>
        </el-form-item>
        <el-form-item label="所属辖区" prop="jurisdiction">
          <el-input v-model="form.jurisdiction" placeholder="请输入所属辖区" />
        </el-form-item>
        <el-form-item label="联系人" prop="contacts">
          <el-input v-model="form.contacts" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="固定电话" prop="fixedTelephone">
          <el-input v-model="form.fixedTelephone" placeholder="请输入固定电话" />
        </el-form-item>
        <el-form-item label="联系人电话" prop="contactsPhone">
          <el-input v-model="form.contactsPhone" placeholder="请输入联系人电话" />
        </el-form-item>
        <el-form-item label="邮政编码" prop="postalCode">
          <el-input v-model="form.postalCode" placeholder="请输入邮政编码" />
        </el-form-item>
        <el-form-item label="学校网站" prop="webPage">
          <el-input v-model="form.webPage" placeholder="请输入学校网站" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSchool, getSchool, delSchool, addSchool, updateSchool } from "@/api/system/school";
import { selectDictLabel, flattenTree } from '@/utils/ruoyi.js'
import { listDept } from "@/api/system/dept";

export default {
  name: "School",
  dicts: ['school_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学校表格数据
      schoolList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schoolName: null,
        schoolType: null,
        schoolAddress: null,
        schoolProfile: null,
        schoolEmil: null,
        schoolType: null,
        upDepartment: null,
        jurisdiction: null,
        contacts: null,
        fixedTelephone: null,
        contactsPhone: null,
        postalCode: null,
        webPage: null,
        status: null,
        isDelete: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      deptTree: [],
      deptList: []
    };
  },
  created() {
    this.getList();
    this.getListDept()
  },
  methods: {
    /** 查询部门列表 */
    getListDept() {
      listDept().then(response => {
        this.deptTree = this.handleTree(response.data, "deptId");
        this.deptList = flattenTree(JSON.parse(JSON.stringify(response.data)))
      });
    },
    /** 查询学校列表 */
    getList() {
      this.loading = true;
      listSchool(this.queryParams).then(response => {
        this.schoolList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sid: null,
        schoolName: null,
        schoolType: null,
        schoolAddress: null,
        schoolProfile: null,
        schoolEmil: null,
        schoolType: null,
        upDepartment: null,
        jurisdiction: null,
        contacts: null,
        fixedTelephone: null,
        contactsPhone: null,
        postalCode: null,
        webPage: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDelete: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学校";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const sid = row.sid || this.ids
      getSchool(sid).then(response => {
        this.form = Object.assign(response.data, { upDepartment: Number(response.data.upDepartment) });
        this.open = true;
        this.title = "修改学校";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.sid != null) {
            updateSchool(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSchool(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sids = row.sid || this.ids;
      this.$modal.confirm('是否确认删除学校编号为"' + sids + '"的数据项？').then(function () {
        return delSchool(sids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/school/export', {
        ...this.queryParams
      }, `school_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
